package com.carbon.trade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carbon.trade.entity.CarbonTradePrice;
import com.carbon.trade.mapper.CarbonTradePriceMapper;
import com.carbon.trade.param.CarbonTradePriceQueryParam;
import com.carbon.trade.service.CarbonTradePriceService;
import com.carbon.trade.vo.CarbonTradePriceQueryVo;
import com.carbon.common.api.Paging;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 碳交易询报价管理 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-12-19
 */
@Service
public class CarbonTradePriceServiceImpl extends ServiceImpl<CarbonTradePriceMapper, CarbonTradePrice> implements CarbonTradePriceService {

    @Override
    public Paging<CarbonTradePriceQueryVo> getPageList(CarbonTradePriceQueryParam queryParam) {
        // 创建分页对象
        Page<CarbonTradePrice> page = new Page<>(queryParam.getCurrent(), queryParam.getSize());
        
        // 构建查询条件
        QueryWrapper<CarbonTradePrice> queryWrapper = new QueryWrapper<>();
        
        // 根据买方机构名称过滤
        if (queryParam.getBuyerName() != null && !queryParam.getBuyerName().isEmpty()) {
            queryWrapper.like("buyer_id", queryParam.getBuyerName());
        }
        
        // 根据卖方机构名称过滤
        if (queryParam.getSellerName() != null && !queryParam.getSellerName().isEmpty()) {
            queryWrapper.like("seller_id", queryParam.getSellerName());
        }
        
        // 根据过期时间范围过滤
        if (queryParam.getExpirationDateStart() != null) {
            queryWrapper.ge("buyer_delivery_time", queryParam.getExpirationDateStart());
        }
        if (queryParam.getExpirationDateEnd() != null) {
            queryWrapper.le("buyer_delivery_time", queryParam.getExpirationDateEnd());
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc("created_time");
        
        // 执行分页查询
        IPage<CarbonTradePrice> result = baseMapper.selectPage(page, queryWrapper);
        
        // 转换为VO对象
        List<CarbonTradePriceQueryVo> voList = new ArrayList<>();
        for (CarbonTradePrice entity : result.getRecords()) {
            CarbonTradePriceQueryVo vo = convertToVo(entity);
            voList.add(vo);
        }
        
        // 返回分页结果
        return new Paging<>(result.getTotal(), result.getCurrent(), voList);
    }

    @Override
    public CarbonTradePriceQueryVo getInfo(Long id) {
        CarbonTradePrice entity = baseMapper.selectById(id);
        if (entity == null) {
            return null;
        }
        return convertToVo(entity);
    }

    @Override
    public boolean intendedTransaction(Object data) {
        // TODO: 实现意向成交逻辑
        return true;
    }

    /**
     * 将实体对象转换为VO对象
     */
    private CarbonTradePriceQueryVo convertToVo(CarbonTradePrice entity) {
        CarbonTradePriceQueryVo vo = new CarbonTradePriceQueryVo();
        vo.setId(entity.getId());
        vo.setTradeQuoteId(entity.getTradeQuoteId());
        vo.setBuyerId(entity.getBuyerId());
        vo.setBuyerTradeQuantity(entity.getBuyerTradeQuantity());
        vo.setBuyerUnitPrice(entity.getBuyerUnitPrice());
        vo.setBuyerDeliveryMethod(entity.getBuyerDeliveryMethod());
        vo.setBuyerDeliveryExchange(entity.getBuyerDeliveryExchange());
        vo.setBuyerDeliveryTime(entity.getBuyerDeliveryTime());
        vo.setSellerId(entity.getSellerId());
        vo.setSellerTradeQuantity(entity.getSellerTradeQuantity());
        vo.setSellerUnitPrice(entity.getSellerUnitPrice());
        vo.setSellerDeliveryMethod(entity.getSellerDeliveryMethod());
        vo.setSellerDeliveryExchange(entity.getSellerDeliveryExchange());
        vo.setSellerDeliveryTime(entity.getSellerDeliveryTime());
        vo.setProjectScope(entity.getProjectScope());
        vo.setProjectScopeCode(entity.getProjectScopeCode());
        vo.setCreatedTime(entity.getCreatedTime());
        
        // 设置默认值
        vo.setConversation("对话" + entity.getId());
        vo.setTradeRole("0270000001"); // 默认买方
        vo.setTradeRoleName("买方");
        vo.setAssetTypeName("碳配额");
        vo.setProjectTypeName("可再生能源");
        vo.setContactsName("联系人" + entity.getId());
        vo.setExpirationDate(entity.getBuyerDeliveryTime());
        vo.setBuyerDeliveryMethodName("线上交割");
        vo.setSellerDeliveryMethodName("线上交割");
        vo.setBuyerDeliveryExchangeName("上海环境能源交易所");
        vo.setSellerDeliveryExchangeName("上海环境能源交易所");
        vo.setBuyerName("买方机构" + entity.getBuyerId());
        vo.setSellerName("卖方机构" + entity.getSellerId());
        
        return vo;
    }
}